package l1j.server.server.templates;

public class L1ChatTemp {

	/**重複喊話對照模組**/

	private final byte[] _text;
	private long _time;
	private int _count =1;
	private static final int MAXTIME = 120000;
	private final int _length;
	private final int _total;

	public L1ChatTemp(final byte[] chatText , final int total){
		this._text = chatText;
		this._time = System.currentTimeMillis();
		_length = this._text.length;
		_total = total;
	}

	public boolean checkChat(final byte[] chatText , final int total){
		if(chatText.length == _length  //長度相同
				&& _total == total){	//資料總數相同
			final int size = chatText.length;
			int i = 0;
			while ((i < size) && (chatText[i] == this._text[i])){
				 i ++;
			}
			if (i == size){
				this.setTime();
				if(this._count >=3){
					return false;
				}
				this._count++;
			}
		}
		return true;
	}

	private void setTime(){
		this._time = System.currentTimeMillis();
	}
	
	private void checkTime(){
		if(System.currentTimeMillis() - this._time >=L1ChatTemp.MAXTIME){
			this._time = 0;
		}
	}
	
	public boolean isActlive(){
		checkTime();
		if(this._time ==0){
			this._count =0;
			return true;
		}
		return false;
	}
}
